/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.server.visitor.impl;

import org.springframework.util.ObjectUtils;

import com.genia.toolbox.web.gwt.form.client.form.ButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.CheckBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.DateItem;
import com.genia.toolbox.web.gwt.form.client.form.DateTimeItem;
import com.genia.toolbox.web.gwt.form.client.form.DynamicListBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.DynamicRadioButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.EnumDescriptableValuedItemEntryList;
import com.genia.toolbox.web.gwt.form.client.form.FileUploadItem;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.form.HorizontalForm;
import com.genia.toolbox.web.gwt.form.client.form.I18nTextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.I18nTextBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.ImageItem;
import com.genia.toolbox.web.gwt.form.client.form.InlineFormMultipleItem;
import com.genia.toolbox.web.gwt.form.client.form.InlineFormSimpleItem;
import com.genia.toolbox.web.gwt.form.client.form.LabelItem;
import com.genia.toolbox.web.gwt.form.client.form.LabeledForm;
import com.genia.toolbox.web.gwt.form.client.form.ListBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.MultipleFileUploadItem;
import com.genia.toolbox.web.gwt.form.client.form.PasswordBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.RadioButtonItem;
import com.genia.toolbox.web.gwt.form.client.form.RichTextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.SimpleValuedItemEntryList;
import com.genia.toolbox.web.gwt.form.client.form.SubFormMultipleItem;
import com.genia.toolbox.web.gwt.form.client.form.SubFormSimpleItem;
import com.genia.toolbox.web.gwt.form.client.form.TabForm;
import com.genia.toolbox.web.gwt.form.client.form.TextAreaItem;
import com.genia.toolbox.web.gwt.form.client.form.TextBoxItem;
import com.genia.toolbox.web.gwt.form.client.form.TimeItem;
import com.genia.toolbox.web.gwt.form.client.form.VerticalForm;
import com.genia.toolbox.web.gwt.form.client.form.impl.AbstractBasicForm;
import com.genia.toolbox.web.gwt.form.client.visitor.FormVisitor;

/**
 * Retrieve a sub-form by its identifier.
 */
public class SpringByNameFormVisitor
    implements FormVisitor
{

  /**
   * Form identifier.
   */
  private String formIdentifier = null;

  /**
   * The form.
   */
  private Form form = null;

  /**
   * Whether the form has been found.
   */
  private boolean isFound = false;



  /**
   * Constructor.
   * 
   * @param formIdentifier
   *          The form identifier.
   */
  public SpringByNameFormVisitor(String formIdentifier)
  {
    super();
    this.formIdentifier = formIdentifier;
  }



  /**
   * Initialise the search.
   * 
   * @param formIdentifier
   *          The form identifier.
   */
  public void initialise(String formIdentifier)
  {
    this.formIdentifier = formIdentifier;
    this.form = null;
    this.isFound = false;
  }



  /**
   * Check whether the form has the right identifier.
   * 
   * @param form
   *          the form.
   */
  private void checkForm(Form form)
  {
    if (!isFound) {
      if (ObjectUtils.nullSafeEquals(form.getName(), this.formIdentifier)) {
        this.form = form;
        this.isFound = true;
      }
    }
  }



  /**
   * Check whether the container form has the right identifier, and then check
   * its childs.
   * 
   * @param form
   *          the form.
   */
  @SuppressWarnings("unchecked")
  private void checkContainerForm(Form form)
  {
    if (!isFound) {
      if (ObjectUtils.nullSafeEquals(form.getName(), this.formIdentifier)) {
        this.form = form;
        this.isFound = true;
      }
      else {
        AbstractBasicForm<Form> basicForm = (AbstractBasicForm<Form>) form;
        for (Form subForm : basicForm.getForms()) {
          if (!isFound) {
            subForm.accept(this);
          }
          else {
            break;
          }
        }
      }
    }
  }



  /**
   * getter for the form property.
   * 
   * @return the form
   */
  public Form getForm()
  {
    return form;
  }



  /**
   * setter for the form property.
   * 
   * @param form
   *          the form to set
   */
  public void setForm(Form form)
  {
    this.form = form;
  }



  /**
   * visitor VerticalForm.
   * 
   * @param verticalForm
   *          a VerticalForm object.
   */
  public void visitVerticalForm(VerticalForm verticalForm)
  {
    this.checkContainerForm(verticalForm);
  }



  /**
   * visit method TabForm.
   * 
   * @param tabForm
   *          a tabForm object.
   */
  public void visitTabForm(TabForm tabForm)
  {
    this.checkContainerForm(tabForm);
  }



  /**
   * visit checkboxItem.
   * 
   * @param checkBoxItem
   *          is of type CheckBoxItem.
   */
  public void visitCheckBoxItem(CheckBoxItem checkBoxItem)
  {
    this.checkForm(checkBoxItem);
  }



  /**
   * visit RadioBoxItem.
   * 
   * @param radioButtonItem
   *          an item of type RAdioButton.
   */
  public void visitRadioButtonItem(RadioButtonItem radioButtonItem)
  {
    this.checkForm(radioButtonItem);
  }



  /**
   * visit textBoxItem.
   * 
   * @param textBoxItem
   *          is of type TextBoxItem.
   */
  public void visitTextBoxItem(TextBoxItem textBoxItem)
  {
    this.checkForm(textBoxItem);
  }



  /**
   * visit textAreaItem.
   * 
   * @param textAreaItem
   *          an item of type TextAreaItem.
   */
  public void visitTextAreaItem(TextAreaItem textAreaItem)
  {
    this.checkForm(textAreaItem);
  }



  /**
   * visit passwordBoxItem.
   * 
   * @param passwordBoxItem
   *          an item of type passwordBox.
   */
  public void visitPasswordBoxItem(PasswordBoxItem passwordBoxItem)
  {
    this.checkForm(passwordBoxItem);
  }



  /**
   * visit HorizontalForm.
   * 
   * @param horizontalForm
   *          an item of type HorizontalForm.
   */
  public void visitHorizontalForm(HorizontalForm horizontalForm)
  {
    this.checkContainerForm(horizontalForm);
  }



  /**
   * visit listBoxItem.
   * 
   * @param listBoxItem
   *          an item of type listBox.
   */
  public void visitListBoxItem(ListBoxItem listBoxItem)
  {
    this.checkForm(listBoxItem);
  }



  /**
   * visit RichTextAreaItem.
   * 
   * @param richtextAreaItem
   *          an item of type RichTextAreaItem.
   */
  public void visitRichTextAreaItem(RichTextAreaItem richtextAreaItem)
  {
    this.checkForm(richtextAreaItem);
  }



  /**
   * visit ImageItem.
   * 
   * @param imageItem
   *          an item of type ImageItem.
   */
  public void visitImageItem(ImageItem imageItem)
  {
    this.checkForm(imageItem);
  }



  /**
   * visit LabelItem.
   * 
   * @param labelItem
   *          an item of type LabelItem.
   */
  public void visitLabelItem(LabelItem labelItem)
  {
    this.checkForm(labelItem);
  }



  /**
   * visit a {@link ButtonItem}.
   * 
   * @param buttonItem
   *          the {@link ButtonItem} to visit
   */
  public void visitButtonItem(ButtonItem buttonItem)
  {
    this.checkForm(buttonItem);
  }



  /**
   * visit {@link SubFormSimpleItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitSubFormSimpleItem(SubFormSimpleItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link SubFormMultipleItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitSubFormMultipleItem(SubFormMultipleItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link FileUploadItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitFileUploadItem(FileUploadItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link MultipleFileUploadItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitMultipleFileUploadItem(MultipleFileUploadItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link DateItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitDateItem(DateItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link TimeItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitTimeItem(TimeItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit {@link DateTimeItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitDateTimeItem(DateTimeItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit a {@link InlineFormSimpleItem}.
   * 
   * @param inlineFormSimpleItem
   *          the {@link InlineFormSimpleItem} to visit
   */
  public void visitInlineFormSimpleItem(InlineFormSimpleItem inlineFormSimpleItem)
  {
    this.checkForm(inlineFormSimpleItem);
  }



  /**
   * visit a {@link InlineFormMultipleItem}.
   * 
   * @param inlineFormMultipleItem
   *          the {@link InlineFormMultipleItem} to visit
   */
  public void visitInlineFormMultipleItem(InlineFormMultipleItem inlineFormMultipleItem)
  {
    this.checkForm(inlineFormMultipleItem);
  }



  /**
   * visit a {@link EnumDescriptableValuedItemEntryList}.
   * 
   * @param valuedItemEntryList
   *          the {@link EnumDescriptableValuedItemEntryList} to visit
   */
  public void visitEnumDescriptableValuedItemEntryList(EnumDescriptableValuedItemEntryList valuedItemEntryList)
  {
    // Do nothing.
  }



  /**
   * visit a {@link SimpleValuedItemEntryList}.
   * 
   * @param valuedItemEntryList
   *          the {@link SimpleValuedItemEntryList} to visit
   */
  public void visitSimpleValuedItemEntryList(SimpleValuedItemEntryList valuedItemEntryList)
  {
    // Do nothing.
  }



  /**
   * visit a {@link LabeledForm}.
   * 
   * @param labeledForm
   *          the {@link LabeledForm} to visit
   */
  public void visitLabeledForm(LabeledForm labeledForm)
  {
    this.checkForm(labeledForm);
    if (!isFound && labeledForm.getSubForm() != null) {
      labeledForm.getSubForm().accept(this);
    }
  }



  /**
   * visit a {@link I18nTextAreaItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitI18nTextAreaItem(I18nTextAreaItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit a {@link I18nTextBoxItem}.
   * 
   * @param item
   *          the item to visit
   */
  public void visitI18nTextBoxItem(I18nTextBoxItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit a {@link DynamicRadioButtonItem}.
   * 
   * @param item
   *          the {@link DynamicRadioButtonItem} to visit
   */
  public void visitDynamicRadioButtonItem(DynamicRadioButtonItem item)
  {
    this.checkForm(item);
  }



  /**
   * visit a {@link DynamicListBoxItem}.
   * 
   * @param item
   *          the {@link DynamicListBoxItem} to visit
   */
  public void visitDynamicListBoxItem(DynamicListBoxItem item)
  {
    this.checkForm(item);
  }

}
